from django.core.management.base import BaseCommand
from movie.models import Movie
import json
import os

class Command(BaseCommand):
    help = '初始化电影'

    def handle(self, *args, **kwargs):
        self.stdout.write(self.style.SUCCESS('开始初始化电影'))
        # 此处应先创建 category.json 文件，以下示例为假设根据 model 生成初始数据的逻辑
        # 获取当前脚本所在目录
        base_dir = os.path.dirname(os.path.abspath(__file__))
        # 拼接 movie.json 文件路径
        json_file_path = os.path.join(base_dir, 'movie.json')

        # 读取 movie.json 文件内容
        with open(json_file_path, 'r', encoding='utf-8') as f:
            Movie_data = json.load(f)
            
        Movie.objects.bulk_create([Movie(**data) for data in Movie_data])
        
        self.stdout.write(self.style.SUCCESS('电影初始化完成'))